package com.cnsuning.jobtool.jobtools.mycat.generator.pre.prexz;

import com.cnsuning.jobtool.utils.ExcelUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.FileUtils;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JbossXmlGenerator {

    public static final String JNDI_NAME = "java:jboss/jdbc/omsd_mysql_DS%s";



    /**
     * 配置文件路径
     */
    private String excelConfigPath = "src/main/java/com/cnsuning/jobtool/jobtools/mycat/generator/pre/prexz/files/config.xlsx";


    /**
     * 最终生成文件路径
     */
    private String targetPath = "src/main/java/com/cnsuning/jobtool/jobtools/mycat/generator/pre/prexz/files/standalone.xml";

    /**
     * 分表模板
     */
    public final String DN_TEMPLATE = "                <datasource jta=\"false\" jndi-name=\""+JNDI_NAME+"\" pool-name=\"mysql-datasource%s\" enabled=\"true\" use-ccm=\"false\">\n" +
            "                    <connection-url>jdbc:mysql://%s:3306/%s?characterEncoding=utf8</connection-url>\n" +
            "                    <driver-class>com.mysql.jdbc.Driver</driver-class>\n" +
            "                    <connection-property name=\"timerLevelForQueryTimeOut\">\n" +
            "                        2\n" +
            "                    </connection-property>\n" +
            "                    <connection-property name=\"connectTimeout\">\n" +
            "                        5000\n" +
            "                    </connection-property>\n" +
            "                    <connection-property name=\"useSSL\">\n" +
            "                        false\n" +
            "                    </connection-property>\n" +
            "                    <driver>com.mysql</driver>\n" +
            "                    <pool>\n" +
            "                        <min-pool-size>30</min-pool-size>\n" +
            "                        <max-pool-size>30</max-pool-size>\n" +
            "                        <flush-strategy>EntirePool</flush-strategy>\n" +
            "                    </pool>\n" +
            "                    <security>\n" +
            "                        <user-name>%s</user-name>\n" +
            "                        <password>%s</password>\n" +
            "                    </security>\n" +
            "                    <validation>\n" +
            "                        <check-valid-connection-sql>select 1</check-valid-connection-sql>\n" +
            "                        <validate-on-match>false</validate-on-match>\n" +
            "                        <background-validation>true</background-validation>\n" +
            "                        <background-validation-millis>60000</background-validation-millis>\n" +
            "                    </validation>\n" +
            "                    <timeout>\n" +
            "                        <idle-timeout-minutes>2</idle-timeout-minutes>\n" +
            "                    </timeout>\n" +
            "                    <statement>\n" +
            "                        <share-prepared-statements>false</share-prepared-statements>\n" +
            "                    </statement>\n" +
            "                </datasource>\n";

    /**
     * 生成datanode和datahost配置
     * @param dbsConfig
     * @throws IOException
     * @throws InvalidFormatException
     */
    private void generateDataNode(List<Map<String, String[]>> dbsConfig) throws IOException, InvalidFormatException {
        if (CollectionUtils.isEmpty(dbsConfig)) return;
        File targetFile = new File(targetPath);

        //生成datahost
        for (int i =0 ; i < dbsConfig.size(); i ++)
        {
            Map<String, String[]> rowData = dbsConfig.get(i);
            for (Map.Entry<String, String[]> entry : rowData.entrySet()){
                String database = entry.getKey();
                String vip = entry.getValue()[0];
                String user = entry.getValue()[1];
                String pass = entry.getValue()[2];
//                <dataNode name="bizMysqlDn_000" dataHost="data_host1" database="omsddb_001"/>
                String  dnStr = String.format(DN_TEMPLATE,(i+1),(i+1),vip,database,user,pass);
                FileUtils.writeStringToFile(targetFile,dnStr,"utf-8",true);
            }
        }
    }


    @Test
    public void generate() throws IOException, InvalidFormatException {
            File targetFile = new File(targetPath);
        if (targetFile.exists())
        {
            targetFile.delete();
            targetFile.createNewFile();
        }


        File configFile = new File(excelConfigPath);

        //datenode
        List<List<Object>> dbsDataList = ExcelUtils.readExcelNoTitleOneSheet(configFile, "dbs");
        List<Map<String, String[]>> dbsConfig = new ArrayList<>();
        for (List<Object> rowData : dbsDataList)
        {
            Map<String,String[]> row = new HashMap<>();
            row.put(rowData.get(0)+"",new String[]{rowData.get(1)+"",rowData.get(2)+"",rowData.get(3)+""});
            dbsConfig.add(row);
        }
        generateDataNode(dbsConfig);
    }
}
